<template>
  <el-card>
    <el-row>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item><a href="/">医生信息管理</a></el-breadcrumb-item>
        <el-breadcrumb-item>医生信息列表</el-breadcrumb-item>
      </el-breadcrumb>
    </el-row>
    <el-row>
      <el-input v-model="queryInfo.query"></el-input>
      <el-button type="primary" @click="getDoctorList()">搜索</el-button>
      <el-button type="warning" @click="addDoctordialogVisible = true"
        >添加用户</el-button
      >
    </el-row>
    <el-table :data="doctorList" border style="width: 100%" stripe>
      <el-table-column prop="name" label="姓名" width="100"> </el-table-column>
      <el-table-column prop="age" label="年龄" width="100"> </el-table-column>
      <el-table-column prop="sex" label="性别" width="100"> </el-table-column>
      <el-table-column
        prop="hospital"
        label="医院"
        width="180"
      ></el-table-column>
      <el-table-column prop="department" label="科室" width="100">
      </el-table-column>
      <el-table-column prop="jobNumber" label="工号"> </el-table-column>
      <el-table-column prop="position" label="职位"> </el-table-column>
      <el-table-column prop="adress" label="门诊地址"> </el-table-column>
      <el-table-column prop="phone" label="手机号"> </el-table-column>
      <el-table-column prop="email" label="邮箱"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="showEditDialog(scope.row._id)"
            >修改</el-button
          >
          <el-button
            type="danger"
            size="mini"
            @click="deleteConfirm(scope.row._id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <el-dialog
      title="添加用户信息"
      :visible.sync="addDoctordialogVisible"
      width="50%"
    >
      <el-form
        :model="addruleForm"
        :rules="addrules"
        ref="addDoctorRef"
        label-width="100px"
      >
        <el-form-item label="姓名" prop="name">
          <el-input v-model="addruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="addruleForm.age"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="addruleForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="addruleForm.email"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio label="男" v-model="addruleForm.sex"></el-radio>
          <el-radio label="女" v-model="addruleForm.sex"></el-radio>
        </el-form-item>
        <el-form-item label="医院" prop="hospital">
          <el-input v-model="addruleForm.hospital"></el-input>
        </el-form-item>
        <el-form-item label="工号" prop="jobNumber">
          <el-input v-model="addruleForm.jobNumber"></el-input>
        </el-form-item>
        <el-form-item label="科室" prop="department">
          <el-input v-model="addruleForm.department"></el-input>
        </el-form-item>
        <el-form-item label="职位" prop="position">
          <el-input v-model="addruleForm.position"></el-input>
        </el-form-item>
        <el-form-item label="门诊地址" prop="adress">
          <el-input v-model="addruleForm.adress"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDoctordialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addConfirm">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="修改信息" :visible.sync="editDialogVisible" width="50%">
      <el-form
        :model="editForm"
        :rules="addrules"
        ref="addDoctorRef"
        label-width="100px"
      >
        <el-form-item label="姓名" prop="name">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="editForm.age"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="editForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="editForm.email"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio label="男" v-model="editForm.sex"></el-radio>
          <el-radio label="女" v-model="editForm.sex"></el-radio>
        </el-form-item>
        <el-form-item label="医院" prop="hospital">
          <el-input v-model="editForm.hospital"></el-input>
        </el-form-item>
        <el-form-item label="科室" prop="department">
          <el-input v-model="editForm.department"></el-input>
        </el-form-item>
        <el-form-item label="职位" prop="position">
          <el-input v-model="editForm.position"></el-input>
        </el-form-item>
        <el-form-item label="门诊地址" prop="adress">
          <el-input v-model="editForm.adress"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editConfirm"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
export default {
  data () {
    return {
      doctorList: [],
      queryInfo: {
        pagenum: 1,
        pagesize: 10,
        query: ''
      },
      total: 0,
      addDoctordialogVisible: false,
      addrules: {
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        age: [
          { required: true, message: '请输入年龄', trigger: 'blur' },
          { min: 2, max: 3, message: '长度在 2 到 3 个字符', trigger: 'blur' }
        ],
        hospital: [
          { required: true, message: '请输入医院名称', trigger: 'blur' }
        ],
        department: [
          { required: true, message: '请输入科室名称', trigger: 'blur' }
        ],
        position: [{ required: true, message: '请输入职位', trigger: 'blur' }],
        adress: [
          { required: true, message: '请输入门诊地址', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            min: 7,
            max: 11,
            message: '长度在 7 到 11 个字符',
            trigger: 'blur'
          }
        ],
        jobNumber: [
          { required: true, message: '请输入工号', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '长度在 6 到 15 个字符',
            trigger: 'blur'
          }
        ]
      },
      addruleForm: {
        name: '',
        age: '',
        sex: '',
        hospital: '',
        department: '',
        position: '',
        adress: '',
        phone: '',
        email: '',
        jobNumber: ''
      },
      editDialogVisible: false,
      editForm: {}
    }
  },
  created () {
    this.getDoctorList()
  },
  methods: {
    getDoctorList () {
      this.$http
        .get('/api/doctor/doctorList', { params: this.queryInfo })
        .then((res) => {
          if (res.status !== 200) {
            return this.$message.error('获取列表失败')
          }
          this.doctorList = res.data.doctorList
          this.total = res.data.total
        })
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getDoctorList()
    },
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getDoctorList()
    },
    addConfirm () {
      console.log(this.addruleForm)
      this.$refs.addDoctorRef.validate((valid) => {
        if (!valid) return
        this.$http
          .post('/api/doctor/addinfo', this.addruleForm)
          .then((res) => {
            if (res.status !== 200) {
              return this.$message.error('添加失败')
            }
            this.addDoctordialogVisible = false
            this.$message.success('添加成功')
            this.getDoctorList()
          })
      })
    },
    async deleteConfirm (id) {
      const confirmResult = await this.$confirm(
        '此操作将永久删除该信息, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消该操作')
      }
      this.$http.delete('/api/doctor/delete/' + id).then((res) => {
        if (res.status !== 200) return this.$message.error('删除失败')
      })
      this.getDoctorList()
      this.$message.success('删除成功')
    },
    async showEditDialog (id) {
      const { data: res } = await this.$http.get('/api/doctor/doctormsg/' + id)
      if (res.status !== 200) return this.$message.error('获取信息失败')
      this.editForm = res.doctor
      this.editDialogVisible = true
    },
    async editConfirm () {
      const { data: res } = await this.$http.post('/api/doctor/edit/' + this.editForm._id,this.editForm)
      if (res.status !== 200) return this.$message.error('修改失败')
      this.editDialogVisible = false
      this.$message.success('修改成功')
      this.getDoctorList()
    }
  }
}
</script>

<style scoped>
.el-table {
  margin-top: 30px;
  margin-bottom: 30px;
}
.el-breadcrumb {
  margin-bottom: 20px;
}
.el-input {
  width: 300px;
  margin-right: 5px;
}
</style>
